package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.clients.CourseClients;
import com.qf.clients.TeacherClients;
import com.qf.config.RedisKey;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.entity.RotationImage;
import com.qf.service.RotationImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @Author: 陌人不故
 * @Date: 2022/10/26/18:29
 */
@RestController
@RequestMapping("/portal")
public class RotationImageController {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RotationImageService rotationImageService;
    @Resource
    CourseClients courseClients;
    @Resource
    TeacherClients teacherClients;

    @RequestMapping("/findRatation")
    public BaseResp findRatation(){
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.ROTATION_IMAGE);
        if (!aBoolean){
            //redis中没有数据
            QueryWrapper<RotationImage> rotationImageQueryWrapper = new QueryWrapper<>();
            rotationImageQueryWrapper.eq("status","start");
            //从数据库里面查询
            List<RotationImage> list = rotationImageService.list(rotationImageQueryWrapper);
            for (RotationImage rotationImage : list) {
                redisTemplate.opsForZSet().add(RedisKey.ROTATION_IMAGE,rotationImage,rotationImage.getImageIndex());
            }
        }
        Set set = redisTemplate.opsForZSet().reverseRange(RedisKey.ROTATION_IMAGE, 0, -1);
        return BaseResp.SUCCESS("查询成功",set,null);
    }


    @RequestMapping("/course/{id}/{page}/{size}")
    public BaseResp course(@PathVariable("id") Integer id,
                           @PathVariable("page") Integer page,
                           @PathVariable("size") Integer size){
       return courseClients.findCourseByClassify(id, page, size);
    }


    @RequestMapping("/findAll")
    public BaseResp findAll(){
        return teacherClients.findAll();
    }
}
